"use strict";
var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
    return typeof obj
} : function (obj) {
    return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
};
!function (global, factory) {
    "object" === ("undefined" == typeof exports ? "undefined" : _typeof(exports)) && "undefined" != typeof module ? module.exports = factory() : "function" == typeof define && define.amd ? define(factory) : (global = global || self).Jdate = factory()
}(void 0, function () {
    !function (css, ref) {
        void 0 === ref && (ref = {});
        var insertAt = ref.insertAt;
        if (css && "undefined" != typeof document) {
            var head = document.head || document.getElementsByTagName("head")[0],
                style = document.createElement("style");
            style.type = "text/css", "top" === insertAt && head.firstChild ? head.insertBefore(style, head.firstChild) : head.appendChild(style), style.styleSheet ? style.styleSheet.cssText = css : style.appendChild(document.createTextNode(css))
        }
    }("ul{margin:0;padding:0}li{list-style-type:none}.jdate-container{font-size:20px;color:#333;text-align:center}.jdate-container header{position:relative;line-height:60px;font-size:18px;border-bottom:1px solid #e0e0e0}.jdate-container .jdate-mask{position:fixed;width:100%;height:100%;top:0;left:0;background:#000;opacity:.4;z-index:999}.jdate-container .jdate-panel{position:fixed;bottom:0;left:0;width:100%;height:273px;z-index:1000;background:#fff;-webkit-animation-duration:.3s;animation-duration:.3s;-webkit-animation-delay:0s;animation-delay:0s;-webkit-animation-iteration-count:1;animation-iteration-count:1}.jdate-container .jdate-btn{position:absolute;left:0;top:0;height:100%;padding:0 15px;color:#666;font-size:16px;cursor:pointer;-webkit-tap-highlight-color:transparent}.jdate-container .jdate-confirm{left:auto;right:0;color:#007bff}.jdate-container .jdate-content{position:relative;top:20px}.jdate-container .jdate-wrapper{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex}.jdate-container .jdate-wrapper>div{position:relative;-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;height:173px;line-height:36px;overflow:hidden;-webkit-flex-basis:-8e;-ms-flex-preferred-size:-8e;flex-basis:-8e;width:1%}.jdate-container .jdate-wrapper ul{position:absolute;left:0;top:0;width:100%;margin-top:68px}.jdate-container .jdate-wrapper li{height:36px}.jdate-container .jdate-dim{position:absolute;left:0;top:0;width:100%;height:68px;background:-o-linear-gradient(bottom,hsla(0,0%,100%,.4),hsla(0,0%,100%,.8));background:-webkit-gradient(linear, left bottom, left top, from(hsla(0, 0%, 100%, 0.4)), to(hsla(0, 0%, 100%, 0.8)));background:-o-linear-gradient(bottom, hsla(0, 0%, 100%, 0.4), hsla(0, 0%, 100%, 0.8));background:linear-gradient(0deg,hsla(0,0%,100%,.4),hsla(0,0%,100%,.8));pointer-events:none;-webkit-transform:translateZ(0);transform:translateZ(0);z-index:10}.jdate-container .mask-top{border-bottom:1px solid #ebebeb}.jdate-container .mask-bottom{top:auto;bottom:1px;border-top:1px solid #ebebeb}.jdate-container .fadeIn{-webkit-animation-name:fadeIn;animation-name:fadeIn}.jdate-container .fadeOut{-webkit-animation-name:fadeOut;animation-name:fadeOut}@-webkit-keyframes fadeIn{0%{bottom:-273px}to{bottom:0}}@keyframes fadeIn{0%{bottom:-273px}to{bottom:0}}@-webkit-keyframes fadeOut{0%{bottom:0}to{bottom:-273px;display:none}}@keyframes fadeOut{0%{bottom:0}to{bottom:-273px;display:none}}@media screen and (max-width:414px){.jdate-container{font-size:18px}}@media screen and (max-width:320px){.jdate-container{font-size:15px}}");
    var $ = void 0 !== $ ? $ : "undefined" != typeof jQuery ? jQuery : null, h = 36;

    function Jdate(config) {
        if ($) {
            if (config && config.el) {
                var _this = this, el = _this.$(config.el);
                if (el && !el.bindJdate && (el.bindJdate = 1, _this.extend(config), _this.tap(el, function () {
                    _this.show()
                }), config.value)) {
                    "input" == el.nodeName.toLowerCase() ? el.value = config.value : el.innerText = config.value;
                    var str = config.value.replace(/-/g, "/").replace(/[^\d/:\s]/g, ""), date = new Date(str);
                    date && "Invalid Date" != date ? el.bindDate = date : console.error("Invalid Date：" + str)
                }
            }
        } else console.error("jQuery is not defined")
    }

    return Jdate.usejQuery = function (jQuery) {
        $ = jQuery
    }, Jdate.prototype = {
        constructor: Jdate, baseData: function () {
            return {
                domId: {
                    YYYY: "jdate-year",
                    MM: "jdate-month",
                    DD: "jdate-day",
                    hh: "jdate-hour",
                    mm: "jdate-min",
                    ss: "jdate-sec"
                },
                opts: {
                    el: "",
                    format: "YYYY-MM-DD",
                    beginYear: 2e3,
                    endYear: 2100,
                    init: null,
                    moveEnd: null,
                    confirm: null,
                    cancel: null,
                    minStep: 1,
                    trigger: "tap",
                    lang: {
                        title: "选择日期",
                        cancel: "取消",
                        confirm: "确认",
                        year: "年",
                        month: "月",
                        day: "日",
                        hour: "时",
                        min: "分",
                        sec: "秒"
                    }
                }
            }
        }, extend: function (config) {
            var opts = this.baseData().opts;
            for (var key in opts) if (opts[key] && "[object Object]" == Object.prototype.toString.call(opts[key])) for (var key2 in config[key]) opts[key][key2] = null == config[key][key2] ? opts[key][key2] : config[key][key2]; else opts[key] = config[key] || opts[key];
            this.config = opts
        }, createUI: function () {
            for (var data = this.baseData(), config = this.config, domId = data.domId, FormatArr = config.format.split(/-|\/|\s|:/g), len = FormatArr.length, ul = "", date = this.$(config.el).bindDate || new Date, lang = config.lang, i = 0; i < len; i++) {
                var f = FormatArr[i];
                if (ul += '<div id="' + domId[f] + '"><ul>', "YYYY" == f) for (var j = config.beginYear; j <= config.endYear; j++) ul += '<li class="' + (j == date.getFullYear() ? "active" : "") + '">' + j + lang.year + "</li>"; else if ("MM" == f) for (var k = 1; k <= 12; k++) ul += '<li class="' + (k == date.getMonth() + 1 ? "active" : "") + '">' + (k < 10 ? "0" + k : k) + lang.month + "</li>"; else if ("DD" == f) for (var day = this.bissextile(date.getFullYear(), date.getMonth() + 1), l = 1; l <= day; l++) ul += '<li class="' + (l == date.getDate() ? "active" : "") + '">' + (l < 10 ? "0" + l : l) + lang.day + "</li>"; else if ("hh" == f) for (var m = 0; m <= 23; m++) ul += '<li class="' + (m == date.getHours() ? "active" : "") + '">' + (m < 10 ? "0" + m : m) + lang.hour + "</li>"; else if ("mm" == f) for (var n = 0; n <= 59; n += config.minStep) ul += '<li class="' + (n == date.getMinutes() ? "active" : "") + '">' + (n < 10 ? "0" + n : n) + lang.min + "</li>"; else if ("ss" == f) for (var o = 0; o <= 59; o++) ul += '<li class="' + (o == date.getSeconds() ? "active" : "") + '">' + (o < 10 ? "0" + o : o) + lang.sec + "</li>";
                ul += "</ul></div>"
            }
            var $html = '<div class="jdate-mask"></div>\n            <div class="jdate-panel fadeIn">\n                <header>\n                    <span class="jdate-btn jdate-cancel">' + lang.cancel + "</span>\n                    " + lang.title + '\n                    <span class="jdate-btn jdate-confirm">' + lang.confirm + '</span>\n                </header>\n                <section class="jdate-content">\n                    <div class="jdate-dim mask-top"></div>\n                    <div class="jdate-dim mask-bottom"></div>\n                    <div class="jdate-wrapper">\n                        ' + ul + "\n                    </div>\n                </section>\n            </div>",
                box = document.createElement("div");
            box.className = "jdate-container", box.innerHTML = $html, document.body.appendChild(box), this.scroll = {};
            for (var _i = 0; _i < len; _i++) {
                var $id = domId[FormatArr[_i]];
                this.scroll[FormatArr[_i]] = $id, this.slide($("#" + $id + ">ul"))
            }
            this.scroll.mm && 1 != config.minStep && $("#" + this.scroll.mm + " li").eq(Math.round(date.getMinutes() / config.minStep)).addClass("active"), $(".jdate-container ul").each(function () {
                var that = $(this), active = that.find(".active");
                if (active.length) {
                    var liTop = -active.position().top;
                    that.animate({top: liTop}, 0)
                }
            })
        }, eventType: function () {
            var isTouch = "ontouchend" in document;
            return {
                isTouch: isTouch,
                tstart: isTouch ? "touchstart" : "mousedown",
                tmove: isTouch ? "touchmove" : "mousemove",
                tend: isTouch ? "touchend" : "mouseup",
                tcancel: isTouch ? "touchcancel" : "mouseleave"
            }
        }, slide: function (el) {
            var _this = this, domId = _this.baseData().domId, lang = _this.config.lang, T = void 0, mT = void 0,
                isPress = !1, events = _this.eventType();

            function dragEnd(that) {
                var t = that.position().top;
                that.css("top", Math.round(t / h) * h + "px"), t = Math.round(Math.abs($(that).position().top));
                var li = that.children("li").get(t / h);
                if ($(li).addClass("active").siblings().removeClass("active"), -1 != [domId.YYYY, domId.MM].indexOf(that.parent().attr("id")) && _this.scroll.DD) {
                    var year = $("#" + domId.YYYY + " .active").text().replace(/\D/g, ""),
                        month = $("#" + domId.MM + " .active").text().replace(/\D/g, "");
                    if (!year || !month) return;
                    var day = _this.bissextile(year, month);
                    if (day != $("#" + domId.DD + " li").length) {
                        var d = $("#" + domId.DD + " .active").text().replace(/\D/g, ""), $html = "",
                            ul = $("#" + domId.DD + " ul");
                        day < d && (d = day);
                        for (var l = 1; l <= day; l++) $html += "<li " + (d == l ? 'class="active"' : "") + ">" + (l < 10 ? "0" + l : l) + lang.day + "</li>";
                        $("#" + domId.DD + " ul").html($html), Math.abs(ul.position().top) > ul.height() - h && ul.css("top", "-" + (ul.height() - h) + "px")
                    }
                }
                _this.config.moveEnd && _this.config.moveEnd.call(_this)
            }

            el.bind(events.tstart, function (e) {
                e.stopPropagation(), e.preventDefault(), e = e.originalEvent, T = e.pageY || e.touches[0].pageY, events.isTouch || (isPress = !0)
            }), el.bind(events.tmove, function (e) {
                var that = $(this);
                if (e.stopPropagation(), e.preventDefault(), e = e.originalEvent, !events.isTouch && !isPress) return !1;
                mT = e.pageY || e.touches[0].pageY, that.css("top", that.position().top + (mT - T) + "px"), T = mT, 0 < that.position().top && that.css("top", "0"), that.position().top < -(that.height() - h) && that.css("top", "-" + (that.height() - h) + "px")
            }), el.bind(events.tend, function (e) {
                var that = $(this);
                e.stopPropagation(), e.preventDefault(), e = e.originalEvent, isPress = !1, dragEnd(that)
            }), el.bind(events.tcancel, function (e) {
                var that = $(this);
                e.stopPropagation(), e.preventDefault(), e = e.originalEvent, isPress = !1, dragEnd(that)
            })
        }, $: function (selector, flag) {
            return "string" != typeof selector && selector.nodeType ? selector : flag ? document.querySelectorAll(selector) : document.querySelector(selector)
        }, tap: function (el, fn) {
            if ("ontouchstart" in window && "tap" == this.config.trigger) {
                var o = {};
                el.addEventListener("touchstart", function (e) {
                    var t = e.touches[0];
                    o.startX = t.pageX, o.startY = t.pageY, o.sTime = +new Date
                }), el.addEventListener("touchend", function (e) {
                    var t = e.changedTouches[0];
                    o.endX = t.pageX, o.endY = t.pageY, +new Date - o.sTime < 300 && Math.abs(o.endX - o.startX) + Math.abs(o.endY - o.startY) < 20 && (e.preventDefault(), fn.call(this, e)), o = {}
                })
            } else el.addEventListener("click", function (e) {
                fn.call(this, e)
            })
        }, show: function () {
            var config = this.config, el = this.$(config.el);
            el.bindJdate && ("input" == el.nodeName.toLowerCase() && el.blur(), this.$(".jdate-container") || config.init && !1 === config.init.call(this) || (this.createUI(), this.event()))
        }, hide: function (flag) {
            var el = this.$(".jdate-panel.fadeIn");
            el && (el.className = "jdate-panel fadeOut", this.destroy(flag))
        }, event: function () {
            var _this = this, mask = _this.$(".jdate-mask"), cancel = _this.$(".jdate-cancel"),
                confirm = _this.$(".jdate-confirm");
            _this.tap(mask, function () {
                _this.hide(1)
            }), _this.tap(cancel, function () {
                _this.hide(1)
            }), _this.tap(confirm, function () {
                var config = _this.config, el = _this.$(config.el), date = config.format, newDate = new Date;
                for (var f in _this.scroll) {
                    var d = $("#" + _this.scroll[f] + " .active").text().replace(/\D/g, "");
                    date = date.replace(f, d), "YYYY" == f ? newDate.setFullYear(d) : "MM" == f ? newDate.setMonth(d - 1) : "DD" == f ? newDate.setDate(d) : "hh" == f ? newDate.setHours(d) : "mm" == f ? newDate.setMinutes(d) : "ss" == f && newDate.setSeconds(d)
                }
                if (config.confirm) {
                    var flag = config.confirm.call(_this, date);
                    if (!1 === flag) return !1;
                    flag && (date = flag)
                }
                "input" == el.nodeName.toLowerCase() ? el.value = date : el.innerText = date, _this.hide(), el.bindDate = newDate
            })
        }, bissextile: function (year, month) {
            var day = void 0;
            return 1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month || 12 == month ? day = 31 : 4 == month || 6 == month || 11 == month || 9 == month ? day = 30 : 2 == month && (day = year % 4 != 0 || year % 100 == 0 && year % 400 != 0 ? 28 : 29), day
        }, destroy: function (flag) {
            var _this = this, config = _this.config;
            flag && config.cancel && config.cancel.call(_this), setTimeout(function () {
                var el = _this.$(".jdate-container");
                document.body.removeChild(el)
            }, 300)
        }
    }, Jdate.version = "2.0.1", Jdate
});